package cn.csc.module.distribute.controller.admin.withdraw;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.distribute.controller.admin.withdraw.vo.*;
import cn.csc.module.distribute.convert.withdraw.DistributeWithdrawConvert;
import cn.csc.module.distribute.dal.dataobject.withdraw.DistributeWithdrawDO;
import cn.csc.module.distribute.service.withdraw.DistributeWithdrawService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 提现记录")
@RestController
@RequestMapping("/distribute/withdraw")
public class DistributeWithdrawController {

    @Resource
    private DistributeWithdrawService withdrawService;

    @PostMapping("/create")
    @ApiOperation("创建提现记录")
    @PreAuthorize("@ss.hasPermission('distribute:withdraw:create')")
    public CommonResult<BigInteger> createWithdraw(@Validated @RequestBody DistributeWithdrawCreateReqVO createReqVO) {
        return success(withdrawService.createWithdraw(createReqVO));
    }

    /**
     *
     *更新提现记录
     *
     */
    @PutMapping("/update")
    @ApiOperation("更新提现记录")
    @PreAuthorize("@ss.hasPermission('distribute:withdraw:update')")
    public CommonResult<Boolean> updateWithdraw(@Validated @RequestBody DistributeWithdrawUpdateReqVO updateReqVO) {
        withdrawService.updateWithdraw(updateReqVO);
        return success(true);
    }

    /**
     *
     *提现成功
     *
     */
    @PostMapping("/applyUpdate")
    @ApiOperation("提现成功")
    @PreAuthorize("@ss.hasPermission('distribute:withdraw:update')")
    public CommonResult<Boolean> applyUpdate(@Validated @RequestBody DistributeWithdrawUpdateReqVO updateReqVO) {
        withdrawService.applyUpdate(updateReqVO);
        return success(true);
    }

    /**
     *
     *提现审核不通过
     *
     */
    @PostMapping("/applyAudit")
    @ApiOperation("提现审核不通过")
    @PreAuthorize("@ss.hasPermission('distribute:withdraw:update')")
    public CommonResult<Boolean> applyAudit(@RequestBody DistributeWithdrawUpdateReqVO updateReqVO) {
        withdrawService.applyAudit(updateReqVO);
        return success(true);
    }

    /**
     *
     *删除提现记录
     *
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除提现记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('distribute:withdraw:delete')")
    public CommonResult<Boolean> deleteWithdraw(@RequestParam("id") BigInteger id) {
        withdrawService.deleteWithdraw(id);
        return success(true);
    }

    /**
     *  获得提现记录详情
     *
     *
     */
    @GetMapping("/get")
    @ApiOperation("获得提现记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('distribute:withdraw:query')")
    public CommonResult<DistributeWithdrawRespVO> getWithdraw(@RequestParam("id") BigInteger id) {
        DistributeWithdrawDO withdraw = withdrawService.getWithdraw(id);
        return success(DistributeWithdrawConvert.INSTANCE.convert(withdraw));
    }

    @GetMapping("/list")
    @ApiOperation("获得提现记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('distribute:withdraw:query')")
    public CommonResult<List<DistributeWithdrawRespVO>> getWithdrawList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<DistributeWithdrawDO> list = withdrawService.getWithdrawList(ids);
        return success(DistributeWithdrawConvert.INSTANCE.convertList(list));
    }

//    @GetMapping("/page")
//    @ApiOperation("获得提现记录分页")
//    @PreAuthorize("@ss.hasPermission('distribute:withdraw:query')")
//    public CommonResult<PageResult<DistributeWithdrawRespVO>> getWithdrawPage(@Valid DistributeWithdrawPageReqVO pageVO) {
//        PageResult<DistributeWithdrawDO> pageResult = withdrawService.getWithdrawPage(pageVO);
//        return success(DistributeWithdrawConvert.INSTANCE.convertPage(pageResult));
//    }

    /**
     *  获得提现记录分页
     *
     *
     */
    @GetMapping("/page")
    @ApiOperation("获得提现记录分页")
    @PreAuthorize("@ss.hasPermission('distribute:withdraw:query')")
    public CommonResult<PageResult<DistributeWithdrawRespVO>> getWithdrawCustomizePage(@Valid DistributeWithdrawPageReqVO pageVO) {
        PageResult<DistributeWithdrawDO> pageResult = withdrawService.getWithdrawCustomizePage(pageVO);
        return success(DistributeWithdrawConvert.INSTANCE.convertPage(pageResult));
    }

    /**
     *  导出提现记录 Excel
     *
     *
     */
    @GetMapping("/export-excel")
    @ApiOperation("导出提现记录 Excel")
    @PreAuthorize("@ss.hasPermission('distribute:withdraw:export')")
    @OperateLog(type = EXPORT)
    public void exportWithdrawExcel(@Valid DistributeWithdrawExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<DistributeWithdrawDO> list = withdrawService.getWithdrawList(exportReqVO);
        // 导出 Excel
        List<DistributeWithdrawExcelVO> datas = DistributeWithdrawConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "提现记录.xls", "数据", DistributeWithdrawExcelVO.class, datas);
    }

}
